package com.example.demo.util;

import static org.apache.poi.ss.usermodel.CellType.STRING;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;


public class ExcelUtil {
        public XSSFSheet sheet;
        public ExcelUtil(File file) throws Exception {
            FileInputStream fileInputStream = null;
            fileInputStream = new FileInputStream(file);
            XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
                //获取sheet
                sheet = sheets.getSheet("Sheet1");

        }

        public String getExcelDateByIndex(int row,int column){
            XSSFRow row1 = sheet.getRow(row);
            row1.getCell(column).setCellType(STRING);
            return row1.getCell(column).toString();
        }

        public String getCellByCaseName(String caseName,int currentColumn,int targetColumn){
            String operateSteps="";
            //获取行数
            int rows = sheet.getPhysicalNumberOfRows();
            for(int i=0;i<rows;i++){
                XSSFRow row = sheet.getRow(i);
                String cell = row.getCell(currentColumn).toString();
                if(cell.equals(caseName)){
                    operateSteps = row.getCell(targetColumn).toString();
                    break;
                }
            }
            return operateSteps;
        }

        //打印excel数据
        public void readExcelData(){
            //获取行数
            int rows = sheet.getPhysicalNumberOfRows();
            for(int i=0;i<rows;i++){
                //获取列数
                XSSFRow row = sheet.getRow(i);
                int columns = row.getPhysicalNumberOfCells();
                for(int j=0;j<columns;j++){
                    String cell = row.getCell(j).toString();
                    System.out.println(cell);
                }
            }
        }
}

